﻿Public Class clsGroupFind
    Dim SecondWait As Integer = 0
    Dim web As WebBrowser
    Dim step_action As String
    Dim ParseHTML As New clsParseHTML
    Dim info As objFindGroup
    Dim current_page As Integer = 1
    Dim limit_page As Integer = 1

    'TODO CHECK TIMEOUT

    Public Sub New()

        web = New WebBrowser
        web.ScriptErrorsSuppressed = True
        AddHandler web.DocumentCompleted, AddressOf web_DocumentCompleted
    End Sub

    Public Sub Find(ByRef infoFind As objFindGroup)
        info = infoFind
        OpenSiteFindGroup(infoFind)
        limit_page = infoFind.Limit / 10
    End Sub

    Public Sub OpenSiteFindGroup(ByRef infoFind As objFindGroup)
        Dim p As Integer = current_page - 1
        Dim s As Integer = p * 10
        Debug.Print("start : " & s)
        web.Navigate("https://m.facebook.com/search/?query=" & ParseHTML.URLEncode(infoFind.GroupName) & "&search=group&pn=2&s=" & s + info.Offset)
    End Sub

    Private Sub web_DocumentCompleted()
        If (frmMain.is_debug = True) Then
            frmDebug.web_debug_findgroup.Document.Write("<html><head><title></title><body>" & web.DocumentText & "</body></html>")
            frmDebug.txt_debug_findgroup.Text = web.DocumentText
        End If
        Dim Result As ArrayList = ParseHTML.getByClassname(web.DocumentText, "td", "bc")
        Dim i As Integer
        Debug.Print(current_page)
        For i = 0 To Result.Count - 1
            Dim obj As New objFindGroup
            Dim link_group As String = ParseHTML.getByAttribute(Result(i), "a", "href")(0).ToString
            link_group = Mid(link_group, InStr(link_group, "/groups/", CompareMethod.Text) + 8, InStr(link_group, "?", CompareMethod.Text) - 8 - InStr(link_group, "/groups/", CompareMethod.Text))
            If (IsNumeric(link_group) = True) Then
                obj.GroupName = ParseHTML.getByAttribute(Result(i), "a", "innerText")(0).ToString
                obj.GroupId = link_group
                Dim num_friend As String = ParseHTML.getByClassname(Result(i), "div", "u bd")(0).ToString
                'Debug.Print(num_friend)
                num_friend = Replace(Mid(num_friend, 1, InStr(num_friend, " ", CompareMethod.Text) - 1), ".", "")
                'Debug.Print(num_friend)
                obj.MemberCount = num_friend
            End If

            If (obj.MemberCount > info.MemberCount) Then
                If (frmMain.GroupFB.GroupFindResult.ContainsKey(obj.GroupId) = False) Then
                    frmMain.GroupFB.GroupFindResult.Add(obj.GroupId, obj)
                    With frmMain.lstFindGroup.Items.Add(frmMain.lstFindGroup.Items.Count + 1)
                        .SubItems.Add(obj.GroupName)
                        .SubItems.Add(obj.MemberCount)
                        .SubItems.Add(obj.GroupId)
                    End With
                End If
            End If
        Next
        If (current_page < limit_page) Then
            current_page = current_page + 1
            Find(info)
        Else
            current_page = 1
        End If

    End Sub
End Class
